package com.boby.lib_common.base

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import android.view.ViewGroup
import android.view.WindowInsetsController
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.boby.lib_common.R
import com.boby.lib_common.dialog.LoadingDialog
import com.lxj.xpopup.impl.LoadingPopupView

abstract  class BaseActivity:AppCompatActivity() {
    private val progressDialog: LoadingPopupView by lazy {
        LoadingDialog.create(this)
    }

     fun showLoading(message: String) {
        LoadingDialog.showLoading(progressDialog, message)
    }

     fun hideLoading() {
        progressDialog.dismiss()
    }

    override fun onCreate(
        savedInstanceState: Bundle?,
        persistentState: PersistableBundle?
    ) {
        super.onCreate(savedInstanceState, persistentState)
        initSystemBar()
    }

    override fun onDestroy() {
        super.onDestroy()
        progressDialog.dismiss()
    }
//    open fun initToolBar(view:View){
//        // 适配状态栏高度
//        ViewCompat.setOnApplyWindowInsetsListener(view) { view, insets ->
//            val statusBarHeight = insets.getInsets(WindowInsetsCompat.Type.statusBars()).top
//            view.layoutParams.height = view.layoutParams.height + statusBarHeight
//            view.setPadding(view.paddingLeft, statusBarHeight, view.paddingRight, view.paddingBottom)
//            insets
//        }
//    }

    open fun initToolBar(view: View) {
        ViewCompat.setOnApplyWindowInsetsListener(view) { view, insets ->
            val statusBarHeight = insets.getInsets(WindowInsetsCompat.Type.statusBars()).top

            // 使用 marginTop 来适配状态栏高度
            val layoutParams = view.layoutParams
            if (layoutParams is ViewGroup.MarginLayoutParams) {
                val originalTopMargin = getViewTag(view, "original_top_margin", layoutParams.topMargin)
                layoutParams.topMargin = originalTopMargin + statusBarHeight
                view.layoutParams = layoutParams
            }

            insets
        }
    }
//    open fun initToolBar(view: View) {
//        // 设置状态栏透明
//        window?.statusBarColor = Color.TRANSPARENT
//        window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
//                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//
//        ViewCompat.setOnApplyWindowInsetsListener(view) { view, insets ->
//            val statusBarHeight = insets.getInsets(WindowInsetsCompat.Type.statusBars()).top
//
//            // 只增加padding，让内容不被状态栏遮挡
//            view.setPadding(
//                view.paddingLeft,
//                view.paddingTop + statusBarHeight,
//                view.paddingRight,
//                view.paddingBottom
//            )
//
//            // 确保背景延伸到状态栏
//            ViewCompat.setBackground(view, view.background)
//
//            insets
//        }
//    }


    private fun getViewTag(view: View, key: String, defaultValue: Int): Int {
        val tagKey = "toolbar_$key".hashCode()
        val tagValue = view.getTag(tagKey)

        if (tagValue == null) {
            view.setTag(tagKey, defaultValue)
            return defaultValue
        }

        return tagValue as Int
    }

    protected open fun initSystemBar(){
        val option = (View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
                )
        window.decorView.systemUiVisibility = option
        window.statusBarColor = Color.TRANSPARENT
        window.navigationBarColor = Color.TRANSPARENT
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.insetsController?.setSystemBarsAppearance(
                WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
            )
        }

    }
}